# -*- coding: utf-8-*-
import datetime

from django.db import models

import uuid


# 图片命名
def images_directory_path(instance, imagename):
    return '/'.join(['image', str(instance.bug.id), str(uuid.uuid4().hex + ".png")])


# 文件命名
def file_directory_path(instance, filename):
    return '/'.join(['file', str(instance.demand.id), filename])


# 专案
class Project(models.Model):
    """专案"""
    id = models.IntegerField(primary_key=True, unique=True)
    # 名称
    name = models.CharField(max_length=50)
    # 路径
    path = models.CharField(max_length=256)
    # Team
    team = models.CharField(max_length=200)
    # 类型
    type = models.CharField(max_length=50, blank=True, null=True)
    # 主要负责人
    DRI = models.CharField('主要负责人', max_length=20)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = verbose_name = '专案'


class BugUser(models.Model):
    """使用者"""
    AUTH_CHOICE = (('user', '用户'), ('admin', '管理员'), ('engineer', '开发者'))
    # 工号
    user_id = models.CharField(max_length=32, primary_key=True, verbose_name='工号')
    # 中文名
    c_name = models.CharField(max_length=32)
    # 英文名
    e_name = models.CharField(max_length=32)
    # 密码
    password = models.CharField(max_length=32)
    # 分机号
    phone = models.IntegerField()
    # 邮箱
    email = models.EmailField(blank=True, null=True)

    # 权限
    authority = models.CharField(max_length=32, choices=AUTH_CHOICE, default=None)
    # Team
    team = models.CharField(max_length=100)
    # 创建时间
    create_time = models.DateField(auto_now_add=True)
    # 修改时间
    Modify_time = models.DateField(auto_now=True)

    def __str__(self):
        return self.c_name

    class Meta:
        verbose_name_plural = verbose_name = '使用者'
        ordering = ['team']


class Bug(models.Model):
    """bug"""
    ASSIGNEE = (('F1232213', '辛双双'), ('23337', 'Francis'))
    # 状态选项
    STATE_CHOICE = (
        ('待指派', '待指派'),
        ('处理中', '处理中'),
        ('已完成', '已完成'),
        ('待验证', '待验证'),
        ('验证有误', '验证有误')
    )
    # 优先级别
    LEVEL_CHOICE = (
        ('低', '低'), ('一般', '一般'), ('紧急', '紧急')
    )
    # 需求类型
    REQUIRE_TYPE = (('数据处理工具', '数据处理工具'), ('网页系统', '网页系统'))
    # 严重程度
    SEVERITY_CHOICE = (
        ('无法工作', '无法工作'), ('可以运行，但数据错误', '可以运行，但数据错误'), ('可以运行，但结果不匹配', '可以运行，但结果不匹配')
    )
    # 出现次数
    EMERGE = (
        ('经常', '经常'), ('偶尔', '偶尔'), ('几率低，5次以下', '几率低，5次以下')
    )

    id = models.IntegerField(primary_key=True, db_column='id', unique=True)
    # 专案
    project = models.ForeignKey('Project', null=True, blank=True, verbose_name=u'专案')
    # 出现次数
    emerge_times = models.CharField(max_length=32, default='', choices=EMERGE, verbose_name=u'出现次数')
    # 版本
    version = models.CharField(max_length=20, blank=True, null=True, verbose_name=u'版本')
    # 状态
    state = models.CharField(max_length=64, choices=STATE_CHOICE, verbose_name=u'状态', default='待指派')

    # 严重程度
    severity = models.CharField(max_length=32, default='', choices=SEVERITY_CHOICE, verbose_name=u'影响程度')
    # 需求种类
    require_type = models.CharField(max_length=32, choices=REQUIRE_TYPE, default='', verbose_name=u'需求类型', blank=True,
                                    null=True)
    # 优先权
    level = models.CharField(max_length=20, choices=LEVEL_CHOICE, default='', verbose_name=u'优先权')
    # 详情描述
    description = models.TextField(null=True, blank=True, verbose_name=u'描述')
    # 难易程度
    difficult = models.CharField(max_length=32, verbose_name=u'难易程度')
    # 预计完成日期
    estime = models.DateTimeField(verbose_name=u'预计完成日期', blank=True, null=True)
    #  图片
    image = models.ImageField(verbose_name=u'图片', blank=True, null=True)

    # 发起者
    owner = models.CharField(max_length=20, verbose_name=u'发起者')
    # 处理者
    assignee = models.CharField(max_length=20, verbose_name=u'处理者', choices=ASSIGNEE, default='')
    # 最后修改bug者
    modify_bug_user = models.CharField(max_length=20, verbose_name=u'最后bug修改者')

    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, verbose_name=u'更新日期')
    # 完成时间
    success_time = models.DateTimeField(verbose_name=u'完成时间', blank=True, null=True)

    def __str__(self):
        return self.owner

    class Meta:
        ordering = ['-modify_time']


class Images(models.Model):
    bug = models.ForeignKey(Bug, default=None)
    image = models.ImageField(upload_to=images_directory_path, verbose_name='Image')

    def __str__(self):
        return self.image

    class Meta:
        db_table = 'Images'


class Version(models.Model):
    """版本"""
    id = models.IntegerField(primary_key=True, unique=True)
    # 版本名
    name = models.CharField(max_length=56)
    # project_id
    project = models.ForeignKey('Project', default='')
    # 创建时间
    create_time = models.DateField(auto_now_add=True, verbose_name=u'创建日期')
    # 修改时间
    modify_time = models.DateField(auto_now=True, verbose_name=u'更新日期')

    class Meta:
        verbose_name_plural = verbose_name = '版本'


class Demand(models.Model):
    """需求"""
    # 需求类型
    REQUIRE_TYPE = (('数据处理工具', '数据处理工具'), ('网页系统', '网页系统'))
    # 紧急程度
    LEVEL_CHOICE = (
        ('低', '低'), ('一般', '一般'), ('紧急', '紧急')
    )
    # 难度
    DIFFICULT = (('低', '低'), ('一般', '一般'), ('高', '高'))
    # 待指派，处理中，已完成，待验证
    STATE = (('待指派', '待指派'), ('处理中', '处理中'), ('已完成', '已完成'), ('待验证', '待验证'))
    # 受理人
    ASSIGNEE = (('F1232213', '辛双双'), ('23337', 'Francis'))

    id = models.IntegerField(primary_key=True, db_column='id', unique=True)
    # 专案 用户自己取名
    name = models.CharField(max_length=256, null=True, blank=True, verbose_name=u'专案名称')

    # 需求种类
    require_type = models.CharField(max_length=32, choices=REQUIRE_TYPE, default='', verbose_name=u'需求类型')
    # 紧急程度
    level = models.CharField(max_length=20, choices=LEVEL_CHOICE, default='', verbose_name=u'紧急程度')
    # 难易程度
    difficult = models.CharField(max_length=32, choices=DIFFICULT, verbose_name=u'难易程度')
    # 状态
    state = models.CharField(max_length=32, choices=STATE, default='待指派', verbose_name=u'状态')

    # 描述
    description = models.TextField(verbose_name=u'描述')
    # 发起人(记录工号)
    owner = models.CharField(max_length=32, verbose_name=u'发起人')
    # 受理人
    assignee = models.CharField(max_length=32, choices=ASSIGNEE, default='', verbose_name=u'受理人')

    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, verbose_name=u'更新日期')
    # 完成时间
    success_time = models.DateTimeField(verbose_name=u'完成时间')
    # 预计完成日期
    estime = models.DateTimeField(verbose_name=u'预计完成日期')
    # 预定开会时间
    meeting_time = models.DateTimeField(verbose_name=u'预定开会时间')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = verbose_name = '需求'
        ordering = ['-create_time']


class Mail(models.Model):
    id = models.IntegerField(primary_key=True)
    # bug id not fk
    bug_id = models.IntegerField(blank=True, null=True)
    # 收信人
    recipient = models.EmailField()
    # project
    project = models.CharField(max_length=200)
    # message (new bug or fixed bug)
    message = models.CharField(max_length=200)
    # state 已发送 or 待发送
    state = models.CharField(max_length=100, default=u'待发送')
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, verbose_name=u'更新日期')

    # # project path
    # project_path = models.CharField(max_length=200, blank=True, null=True)

    class Meta:
        db_table = 'Mail'
        verbose_name_plural = verbose_name = '邮箱'
        ordering = ['-create_time']


class Files(models.Model):
    """
    上传的文件
    """
    demand = models.ForeignKey(Demand, default=None)
    file = models.ImageField(upload_to=file_directory_path, verbose_name='文件')
    name = models.CharField(max_length=200, verbose_name='文件名')
    # 创建时间
    create_time = models.DateTimeField(auto_now_add=True, verbose_name=u'创建日期')
    # 修改时间
    modify_time = models.DateTimeField(auto_now=True, verbose_name=u'更新日期')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'FILES'
        verbose_name_plural = verbose_name = "文件"
        ordering = ['-create_time']